package com.fenyin.samples.study.quartz;  

import java.text.ParseException;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

/**
 * 
 *@Title:  
 *@Description:  编程式调度器
 *@Author:fengzhichao  
 *@Since:2012-9-7  
 *@Version:1.1.0
 */
public class ProgramScheduler {
	
	private static final Log logger = LogFactory.getLog(ProgramScheduler.class);
	
	private Scheduler scheduler;
	
	private final ScheduledExecutorService schedulerExecutor = Executors.newScheduledThreadPool(10);
	
	public ProgramScheduler(){
		try {
			this.scheduler = StdSchedulerFactory.getDefaultScheduler();
		} catch (SchedulerException e) {
			logger.error("ProgramScheduler create error",e);
		}
	}
	
	public void scheduleJob() throws SchedulerException, ParseException{
		//create and init JobDetail instance
		JobDetail jobDetail = new JobDetail("scan_directory_job", Scheduler.DEFAULT_GROUP,ScanDirectoryJob.class);
		jobDetail.getJobDataMap().put("SCAN_DIR", "D:\\temp");
		
		//create and init Trigger instance
//		Trigger trigger = TriggerUtils.makeSecondlyTrigger(10);
		CronTrigger trigger= new CronTrigger("scan_directory_trigger",null,"0 30 17 * * ?");
		trigger.setName("scan_directory_trigger");
		trigger.setStartTime(new Date());
		
		scheduler.scheduleJob(jobDetail, trigger);
	}
	
	public void rescheduleJob() throws SchedulerException, ParseException{
		String triggerName = "scan_directory_trigger";
		CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerName, Scheduler.DEFAULT_GROUP);
		String cronExpression = "0 40 17 * * ?";
		trigger.setCronExpression(cronExpression);
		
		scheduler.rescheduleJob(triggerName, Scheduler.DEFAULT_GROUP, trigger);
	}
	
	public void rescheduleJob(String cronExpression)throws SchedulerException, ParseException{
		String triggerName = "scan_directory_trigger";
		CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerName, Scheduler.DEFAULT_GROUP);
		trigger.setCronExpression(cronExpression);
		
		scheduler.rescheduleJob(triggerName, Scheduler.DEFAULT_GROUP, trigger);
	}
	
	public void scheduleStart() throws SchedulerException{
		logger.info("ProgramScheduler is staring....");
		scheduler.start();
	}
	
	public static void main(String[] args){
		ProgramScheduler scheduler = null;
		try {
			scheduler = new ProgramScheduler();
			scheduler.scheduleJob();
			
			scheduler.scheduleStart();
			
			System.out.println("first reschedule....");
			scheduler.rescheduleJob("0 25 15 * * ?");
			Thread.sleep(60000);
			
//			System.out.println("secend reschedule....");
//			scheduler.rescheduleJob("0 50 17 * * ?");
//			Thread.sleep(60000);
			
//			System.out.println("third reschedule....");
//			scheduler.rescheduleJob("0 52 17 * * ?");
		} catch (SchedulerException e) {
			logger.error("ProgramScheduler scheduleJob error",e);
		} catch (ParseException e) {
			logger.error("ProgramScheduler crontrigger parse error",e);
		} catch (InterruptedException e) {
			logger.error("Thread error",e);
		}
	}
}
